#删除第12131之后的数据
from elasticsearch import Elasticsearch

# 连接到Elasticsearch实例
es = Elasticsearch(['http://10.10.4.165:9200'])  # 请根据您的实际情况修改Elasticsearch实例的URL

# 定义要查询的索引名称
index_name = "&haikou.air_vocs_hour_stats"

# 执行初始查询，设置scroll参数
result = es.search(index=index_name, body={"size": 1000}, scroll='1m')

# 删除第12131之后的数据
count = 0
delete_ids = []

for hit in result['hits']['hits']:
    count += 1
    if count > 12131:
        delete_ids.append(hit['_id'])

while result['_scroll_id'] and count < result['hits']['total']['value']:
    result = es.scroll(scroll_id=result['_scroll_id'], scroll='1m')
    for hit in result['hits']['hits']:
        count += 1
        if count >= 12131:
            delete_ids.append(hit['_id'])

for delete_id in delete_ids:
    es.delete(index=index_name, id=delete_id)

print("删除第12131之后的数据完成")

